//
//  var.m
//  test
//
//  Created by 任文超 on 14-8-18.
//  Copyright (c) 2014年 任文超. All rights reserved.
//

#import "var.h"

@implementation var

// 全局缓存
float verIOS;
CGSize sizeMain;

// read write
void ns_write(NSString *key, NSString *value)
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:value forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

NSString* ns_read(NSString *key)
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

int ns_readasint(NSString *key)
{
    return [ns_read(key) intValue];
}

void ns_writeData(NSString *key, id value)
{
    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:[NSKeyedArchiver archivedDataWithRootObject:value] forKey:key];
    [userData synchronize];
}

id ns_readData(NSString *key)
{
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

// alert
void showAlert(NSString* title, NSString* value)
{
    [[[UIAlertView alloc]initWithTitle:title message:value delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
}

// mask
bool isMask = NO;
MBProgressHUD *mask = nil;

void addMask()
{
    if (!isMask) {
        isMask = YES;
        mask = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] keyWindow]];
        [[[UIApplication sharedApplication] keyWindow] addSubview:mask];
        mask.dimBackground = YES;
        [mask show:YES];
    }
}

void clearMask()
{
    if (isMask) {
        isMask = NO;
        if (mask) {
            [mask show:NO];
            [mask removeFromSuperview];
            mask = nil;
        }
    }
}

MBProgressHUD *promptHud = nil;

void showPrompt(NSString* prompt, int right, float time)
{
    if (promptHud) {
        [promptHud removeFromSuperview];
        promptHud = nil;
    }
    
    promptHud = [[MBProgressHUD alloc] initWithView:[[UIApplication sharedApplication] keyWindow]];
    [[[UIApplication sharedApplication] keyWindow] addSubview:promptHud];
    promptHud.labelText = prompt;
    promptHud.mode = MBProgressHUDModeCustomView;
    if (1 == right) {
        promptHud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right"]];
    }
    else if (2 == right) {
        promptHud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wrong"]];
    }
    [promptHud showAnimated:YES whileExecutingBlock:^{
        sleep(time);
    } completionBlock:^{
        [promptHud removeFromSuperview];
        promptHud = nil;
    }];
}

// net
bool checkNet()
{
    if (FIX_CHECK_NET) {
        if (1 == FIX_CHECK_NET) {
            return true;
        }
        else if (2 == FIX_CHECK_NET) {
            return false;
        }
    }
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus] && ![[Reachability reachabilityForLocalWiFi] currentReachabilityStatus]) {
        return false;
    }
    else {
        return true;
    };
}

@end
